var util = require('../../utils/util.js');
var gitee = require('../../utils/gitee.js');
var github = require('../../utils/github.js');
const db = wx.cloud.database();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searched:false,//是否已经在搜索完毕状态
    key:"",//搜索的key
    resultList: [],
    historyList:[],
    hotList:[
      { key: "小程序", active: false},
      { key: "商城", active: false},
      { key: "JEEXJJ", active: false},
      { key: "java", active: false}
    ]
  },
  gotoArticle: function (e) {
    var idx = e.currentTarget.dataset.index;
    var item = this.data.resultList[idx];
    let data = JSON.stringify(item);
    wx.navigateTo({
      url: '/pages/article/article?data=' + data
    })
  },

  //监听查询输入框
  inputkey: function (event)
  {
    this.setData({
      key: event.detail.value
    });
  },

  //清空查询输入框
  clearQuery:function()
  {
    this.setData({
      key: ""
    });
  },

  //点击历史
  selectHis: function (e) {
    var selKey = e.currentTarget.dataset.key;
    this.setData({
      key: selKey,
      searched:true
    });

    this.doSearch();
  },
  //点击热门搜索
  selectKey: function (e) {
    var idx = e.currentTarget.dataset.index;
    var selKey = this.data.hotList[idx].key;

    //遍历所有，把它们都设置为未选中
    this.data.hotList = this.data.hotList.map(function (item) {
      item.active=false;
      return item;
    });
    //点亮选中的关键词
    this.data.hotList[idx].active=true;
    this.setData({
      key: selKey,
      hotList: this.data.hotList
    });
    this.doSearch();
  },

  //查看搜索历史
  searchHistory: function () {
    var keYstorage = wx.getStorageSync('historyKey');
    var hisArr = null;
    if (keYstorage)
    {
      hisArr = keYstorage.split(",");
    }else{
      hisArr = [];
    }

    this.setData({
      searched: false,
      resultList: [],
      historyList: hisArr
    });
  },

  //清空历史记录
  clearHis:function()
  {
    wx.removeStorageSync("historyKey");
    this.searchHistory();
  },

  /**
   * 点击搜索
   */
  doSearch: function () {

    let that = this;
    var q = this.data.key;
    if (null == q  || "" == q)
    {
      util.showErrorToast("请输入搜索信息");
      return ;
    }

    var keYstorage = wx.getStorageSync('historyKey');
 
    //查看缓存内容
    if (keYstorage) {
      if (this.data.historyList.indexOf(q)==-1)
      {
        keYstorage = q + "," + keYstorage;
        //记录搜索历史
        wx.setStorageSync("historyKey", keYstorage);
      }
    } else {
      wx.setStorageSync("historyKey", q);
    }
    
    //遍历热门搜索,如果查的正是热门搜索的内容，则点亮标签
    this.data.hotList = this.data.hotList.map(function (item) {
      if(q == item.key)
      {
        item.active = true;
      }else{
        item.active = false;
      }
      return item;
    });

    that.setData({ resultList: [], hotList:this.data.hotList,searched: false });

    wx.showLoading({
      title: '拼命查询中',
    });
    

    var radomParam = "&dd=" + new Date().getTime();
    
    //查询码云
    wx.request({
      url: 'https://m.gitee.com/search?q=' + q,
      success: function (res) {
        var giteeProList = gitee.parseHtml(res);

        wx.hideLoading();
        that.setData({ resultList: giteeProList, searched: true });

      },
      fail:function(error) {
        wx.hideLoading();
        that.setData({ resultList: [], searched: true });
      },
    })
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    this.searchHistory();
  },
  onPullDownRefresh: function () {
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})